<?php

use App\Http\Controllers\EventController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\VenueController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
| 
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
| 
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// 用户认证相关接口
Route::prefix('auth')->group(function () {
    Route::post('login', [App\Http\Controllers\UserController::class, 'login']);
    Route::post('login/phone', [App\Http\Controllers\UserController::class, 'loginWithSms']);
    Route::post('register', [App\Http\Controllers\UserController::class, 'register']);
    Route::post('reset-password', [App\Http\Controllers\UserController::class, 'resetPassword']);
    
    // 需要认证的接口
    Route::middleware('auth:sanctum')->group(function () {
        Route::get('me', [App\Http\Controllers\UserController::class, 'me']);
    });
});

// 用户登出
Route::post('/auth/logout', [App\Http\Controllers\UserController::class, 'logout'])->middleware('auth:sanctum');

// 注销账号
Route::delete('/users/delete-account', [App\Http\Controllers\UserController::class, 'deleteAccount'])->middleware('auth:sanctum');

// 其他业务接口
Route::prefix('events')->group(function () {
    Route::get('', [App\Http\Controllers\EventController::class, 'index']);
    Route::get('{id}', [App\Http\Controllers\EventController::class, 'show']);
    
    // 需要管理员权限的接口
    Route::middleware('auth:sanctum', 'admin')->group(function () {
        Route::post('', [App\Http\Controllers\EventController::class, 'store']);
        Route::put('{id}', [App\Http\Controllers\EventController::class, 'update']);
        Route::delete('{id}', [App\Http\Controllers\EventController::class, 'destroy']);
    });
});

// 场馆相关接口
Route::prefix('venues')->group(function () {
    Route::get('', [App\Http\Controllers\VenueController::class, 'index']);
    Route::get('{id}', [App\Http\Controllers\VenueController::class, 'show']);
    
    // 需要认证的管理员接口
    Route::middleware('auth:sanctum', 'admin')->group(function () {
        Route::post('', [App\Http\Controllers\VenueController::class, 'store']);
        Route::put('{id}', [App\Http\Controllers\VenueController::class, 'update']);
        Route::delete('{id}', [App\Http\Controllers\VenueController::class, 'destroy']);
    });
});



// 订单相关接口
Route::prefix('orders')->middleware('auth:sanctum')->group(function () {
    Route::post('', [App\Http\Controllers\OrderController::class, 'store']);
    Route::get('', [App\Http\Controllers\OrderController::class, 'index']);
    Route::get('{id}', [App\Http\Controllers\OrderController::class, 'show']);
    Route::put('{id}', [App\Http\Controllers\OrderController::class, 'update']);
    Route::delete('{id}', [App\Http\Controllers\OrderController::class, 'destroy']);
});

// 测试接口 - 基础信息
Route::get('/test', function () {
    return response()->json([
        'message' => 'API 测试成功！',
        'status' => 'success',
        'timestamp' => now(),
        'version' => '1.0.0'
    ]);
});

// 健康检查接口
Route::get('/health', [HealthController::class, 'check']);

// 用户管理接口 - 需要管理员权限
Route::prefix('users')->middleware('auth:sanctum', 'admin')->group(function () {
    Route::get('', [App\Http\Controllers\UserController::class, 'index']);
    Route::get('{id}', [App\Http\Controllers\UserController::class, 'show']);
    Route::post('', [App\Http\Controllers\UserController::class, 'store']);
    Route::put('{id}', [App\Http\Controllers\UserController::class, 'update']);
    Route::delete('{id}', [App\Http\Controllers\UserController::class, 'destroy']);
    // 测试路由：直接获取所有用户原始数据
    Route::get('raw/all', [App\Http\Controllers\UserController::class, 'getAllUsersRaw']);
});

// 获取服务器信息
Route::get('/info', function () {
    return response()->json([
        'server' => [
            'php_version' => PHP_VERSION,
            'laravel_version' => app()->version(),
            'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
            'memory_usage' => memory_get_usage(true),
            'memory_peak' => memory_get_peak_usage(true)
        ],
        'timestamp' => now()
    ]);
});

// 测试 POST 请求
Route::post('/test-post', function (Request $request) {
    return response()->json([
        'message' => 'POST 请求测试成功HAHAHAHAHAHA',
        'received_data' => $request->all(),
        'method' => $request->method(),
        'timestamp' => now()
    ]);
});

// 测试 PUT 请求
Route::put('/test-put/{id}', function (Request $request, $id) {
    return response()->json([
        'message' => 'PUT 请求测试成功',
        'id' => $id,
        'received_data' => $request->all(),
        'method' => $request->method(),
        'timestamp' => now()
    ]);
});

// 测试 DELETE 请求
Route::delete('/test-delete/{id}', function ($id) {
    return response()->json([
        'message' => 'DELETE 请求测试成功',
        'deleted_id' => $id,
        'timestamp' => now()
    ]);
});

// 模拟用户数据接口已删除，现在使用正确连接数据库的UserController::index方法

// 模拟产品数据接口
Route::get('/products', function () {
    $products = [
        [
            'id' => 1,
            'name' => 'iPhone 15',
            'price' => 7999.00,
            'category' => '手机',
            'stock' => 100
        ],
        [
            'id' => 2,
            'name' => 'MacBook Pro',
            'price' => 12999.00,
            'category' => '电脑',
            'stock' => 50
        ],
        [
            'id' => 3,
            'name' => 'AirPods Pro',
            'price' => 1999.00,
            'category' => '耳机',
            'stock' => 200
        ]
    ];
    
    return response()->json([
        'data' => $products,
        'total' => count($products),
        'message' => '产品列表获取成功'
    ]);
});

// 分页测试接口
Route::get('/posts', function (Request $request) {
    $page = $request->get('page', 1);
    $perPage = $request->get('per_page', 10);
    
    // 模拟数据
    $posts = [];
    for ($i = 1; $i <= 25; $i++) {
        $posts[] = [
            'id' => $i,
            'title' => "测试文章 {$i}",
            'content' => "这是第 {$i} 篇文章的内容...",
            'author' => "作者 {$i}",
            'created_at' => now()->subDays($i)->format('Y-m-d H:i:s')
        ];
    }
    
    $total = count($posts);
    $offset = ($page - 1) * $perPage;
    $paginatedPosts = array_slice($posts, $offset, $perPage);
    
    return response()->json([
        'data' => $paginatedPosts,
        'pagination' => [
            'current_page' => (int)$page,
            'per_page' => (int)$perPage,
            'total' => $total,
            'last_page' => ceil($total / $perPage),
            'from' => $offset + 1,
            'to' => min($offset + $perPage, $total)
        ],
        'message' => '文章列表获取成功'
    ]);
});

// 错误测试接口
Route::get('/error-test', function () {
    return response()->json([
        'error' => '这是一个测试错误',
        'code' => 400,
        'message' => '请求参数错误'
    ], 400);
});

// 延迟测试接口
Route::get('/delay-test', function () {
    sleep(2); // 模拟延迟
    return response()->json([
        'message' => '延迟测试完成',
        'delay' => '2秒',
        'timestamp' => now()
    ]);
});
